<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 支付模型
 * @author www.dingwei.cn
 */

class MerchantPayModel extends MY_Model{
	
	public $tbPay = 'merchant_bonus_pay';

    /**
     * @param $data
     * @param int $code
     * @return bool|int
     * @author khj
     */
    public function savePayOrder($data,$code = 0){
        if ($code){
            $this->db->where('code',$code);
            $query = $this->update($this->tbPay, $data);
            return $query === false ? false : true;
        }else{
            return $this->insert($this->tbPay, $data);
        }
    }

    /**
     * 获取预支付订单信息
     * @param int $merchantId
     * @param int $code
     * @return array
     * @author khj
     */
    public function getInfo($merchantId = 0,$code = 0){
        if (! $code){
            return array();
        }

        if ($merchantId){
            $this->db->where('merchant_id',$merchantId);
        }
        if ($code){
            $this->db->where('code',$code);
        }

        return $this->getOne($this->tbPay);
    }

	
	/**
	 * 支付宝支付
     * @param  $code 预支付订单号
     * @param  $amount  金额
	 */
	public function _getAliPay($code,$amount){

		include APPPATH.'libraries/AliPay/AopSdk.php';
		$config = getCustomConfig('alipay',get_instance()->port);

		$aop = new AopClient;
		$aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
		$aop->appId = $config['appId'];
		$aop->rsaPrivateKey = $config['rsaPrivateKey'];
		$aop->alipayrsaPublicKey = $config['alipayrsaPublicKey'];
		$aop->format = "json";
		$aop->charset = "UTF-8";
		$aop->signType = "RSA2";

		//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
		$request = new AlipayTradeAppPayRequest();
		//SDK已经封装掉了公共参数，这里只需要传入业务参数
		$bizcontent = json_encode(array(
				'body'=>'发放商盟红包',
				'subject'=>'诚信商盟',
				'out_trade_no'=>$code,
				'timeout_express'=>'30m',
				'total_amount'=>$amount,
				'product_code'=>'QUICK_MSECURITY_PAY',
		));
		
		$request->setNotifyUrl(getUrl('app/pay/zfbCallback'));
		$request->setBizContent($bizcontent);
		//这里和普通的接口调用不同，使用的是sdkExecute
		$response = $aop->sdkExecute($request);

		return $response;//就是orderString 可以直接给客户端请求，无需再做处理。
	}


	/**
	 * 微信支付
     * @param  $code 预支付订单号
     * @param  $amount  金额
	 * @return
	 */
	public function _getWxPay($code,$amount){

		include APPPATH.'libraries/WxpayAPI2/lib/WxPay.Api.php';
		include APPPATH.'libraries/WxpayAPI2/example/WxPay.JsApiPay.php';

		$input = new WxPayUnifiedOrder();
		
		$input->SetBody('发放商盟红包');
		$input->SetOut_trade_no($code);
		$input->SetTotal_fee($amount * 100);
		$input->SetTime_start(date("YmdHis"));
		$input->SetTime_expire(date("YmdHis", time() + 60 * 10));
		$input->SetNotify_url(getUrl('app/pay/wxCallback'));//回调
		$input->SetTrade_type('APP');
        $wxpay = WxPayApi::unifiedOrder($input);

		$parameters = array();
		if(! empty($wxpay) && $wxpay['return_code'] == 'SUCCESS' && $wxpay['result_code'] == 'SUCCESS'){
			$tools = new JsApiPay();
			$parameters = $tools->GetAPPParameters($wxpay);

		}
		
		return $parameters;
	}


}